#!/bin/bash
set -o pipefail

# Assuming docker minor and patch version backward-compatibility,
# but not major version compatibility (require 1.X.*).

VERSION_MINOR_MIN_DOCKER_ENGINE="11"
VERSION_MINOR_MIN_DOCKER_COMPOSE="6"

compare_versions() {
  VERSION_MAJOR=$1
  VERSION_ACTUAL=$2
  VERSION_MINIMUM=$3

  if [[ -z $VERSION_ACTUAL ]]; then
    echo "ERROR: Actual version is empty (hint: is it installed, or version format change?)."
    return 1
  fi

  if [ "$VERSION_MAJOR" == "2" ]; then
    return 0
  fi

  if [ "$VERSION_MAJOR" != "1" ]; then
    echo "ERROR: Unsupported major version: $VERSION_MAJOR"
    return 1
  fi

  if [ "$VERSION_ACTUAL" -lt "$VERSION_MINIMUM" ]; then
    echo "ERROR: Actual version installed ($VERSION_ACTUAL) is smaller than minimum requirement ($VERSION_MINIMUM)."
    return 1
  fi

  return 0
}

# test that user has rights to use docker

DOCKER_ENGINE_INFO_STDERR=$(docker info 2>&1 >/dev/null)
DOCKER_ENGINE_INFO_RETVAL=$?
if [[ $DOCKER_ENGINE_INFO_RETVAL -ne 0 ]]; then
  >&2 echo "Local user has insufficient permissions to connect to the docker daemon."
  >&2 echo "Please add user to 'docker' group to be able to access it or check the documentation for your host OS."
  >&2 echo "Be aware that if you were just added to the group, you need to log out and in again."
  >&2 echo $DOCKER_ENGINE_INFO_STDERR
  exit 1
fi

# test Docker Engine version

DOCKER_ENGINE_VERSION_STRING=$(docker info 2>/dev/null | grep "Server Version")

DOCKER_ENGINE_VERSION_NEW_SCHEME_REGEX="Server Version: ([0-9]{2,})|([2-9]+)\.[0-9]+\.[0-9]+"

if [[ $DOCKER_ENGINE_VERSION_STRING =~ $DOCKER_ENGINE_VERSION_NEW_SCHEME_REGEX ]]; then

  :
  # we support all versions in new version scheme, at least for now
  # more info: https://blog.docker.com/2017/03/docker-enterprise-edition/

else
  DOCKER_ENGINE_VERSION_REGEX_MINOR_BACKREF="Server Version: 1\.([0-9]+)\.[0-9]+"
  DOCKER_ENGINE_VERSION_MINOR=$(echo "$DOCKER_ENGINE_VERSION_STRING" | sed -En "s/$DOCKER_ENGINE_VERSION_REGEX_MINOR_BACKREF/\1/p")

  compare_versions "$DOCKER_ENGINE_VERSION_MINOR" "$VERSION_MINOR_MIN_DOCKER_ENGINE"

  if [ $? -ne 0 ]; then
    echo "ERROR: Docker Engine must be on version 1.$VERSION_MINOR_MIN_DOCKER_ENGINE or larger, but found version 1.$DOCKER_ENGINE_VERSION_MINOR installed."
    echo "DOCKER_ENGINE_VERSION_STRING=$DOCKER_ENGINE_VERSION_STRING"
    exit 1
  fi
fi


# test Docker Compose version

DOCKER_COMPOSE_COMMAND=$(docker compose version &>/dev/null && echo 'docker compose' || echo 'docker-compose')
DOCKER_COMPOSE_VERSION_REGEX_BACKREF="v?([0-9]+)\.([0-9]+)\.[0-9]+.*"
DOCKER_COMPOSE_VERSION_STRING=$(${DOCKER_COMPOSE_COMMAND} version --short)
DOCKER_COMPOSE_VERSION_MAJOR=$(echo "$DOCKER_COMPOSE_VERSION_STRING" | sed -En "s/$DOCKER_COMPOSE_VERSION_REGEX_BACKREF/\1/p")
DOCKER_COMPOSE_VERSION_MINOR=$(echo "$DOCKER_COMPOSE_VERSION_STRING" | sed -En "s/$DOCKER_COMPOSE_VERSION_REGEX_BACKREF/\2/p")

compare_versions "$DOCKER_COMPOSE_VERSION_MAJOR" "$DOCKER_COMPOSE_VERSION_MINOR" "$VERSION_MINOR_MIN_DOCKER_COMPOSE"

if [ $? -ne 0 ]; then
  echo "ERROR: Docker Compose must be on version 1.$VERSION_MINOR_MIN_DOCKER_COMPOSE or larger, but found version ${DOCKER_COMPOSE_VERSION_STRING} installed."
  echo "DOCKER_COMPOSE_VERSION_STRING=$DOCKER_COMPOSE_VERSION_STRING"
  exit 1
fi

exit 0
